import React, { Component } from 'react';
import { View, Text, Image, TouchableOpacity, StyleSheet, PixelRatio, TextInput } from 'react-native';
import { orderBaseStyle } from '../../create/_common/style';
import { TitleCpt } from '../../create/_components/TitleCpt';
import { DeliveryCpt } from '../../create/_components/DeliveryCpt';
import ToastUtil from '../../../../utils/toast';
import MethodUtil from '../../../../utils/MethodUtil';
export class DeliveryBlock extends Component {
	constructor(props) {
		super(props);
		let { editData, resultQuantity } = this.props.data || {};
		this.itemData = { planId: null, deliveryBeginDate: null, deliveryEndDate: null, quantity: resultQuantity || null };
		let newItemData = Object.assign({}, this.itemData);

		this.state = {
			planData: editData || [newItemData]
		}
	}

	componentWillReceiveProps (props) {
		let { editData, resultQuantity, user } = props.data || {};
		//if (!editData) return;
		//初始化赋值
		this.itemData.quantity = resultQuantity || ''
		let newItemData = Object.assign({}, this.itemData);
		if (editData && !editData.length) { editData = false; }
		if (!user) {
			if (editData && editData.length == 1) {
				editData[0].quantity = resultQuantity || '';
			} else if (editData && editData.length > 1) {
				let lsZhi = MethodUtil.countObj.subtract(resultQuantity, this._duoFn(editData)) + '';
				editData[editData.length - 1].quantity = lsZhi || '';
			}
		}

		//if (editData && editData.length) editData[0].quantity = editData[0].quantity || resultQuantity || '';
		this.setState({
			planData: editData || [newItemData]
		})
	}
	//闭口多个交货计划取剩余量
	_duoFn = (editData) => {
		let douResult = 0;
		editData && editData.forEach((item, index) => {
			if (index < editData.length - 1) {
				douResult = MethodUtil.countObj.add(item.quantity, douResult)
			}
		})
		return douResult + ''
	}

	// 取剩余的
	_more = () => {
		let { resultQuantity, isEdit } = this.props.data || {};
		if (!isEdit) return;
		let { planData } = this.state;
		let result = 0;
		console.log('editData', planData)
		planData && planData.forEach(item => {
			result = MethodUtil.countObj.add(item.quantity, result)
		});
		// if (resultQ <= 0) {
		// 	resultQ = false;
		// 	ToastUtil.showWarning('交货计划总量大于商品总量')
		// } else {
		// 	resultQ = resultQ + '';
		// }

		let lsresult = MethodUtil.countObj.subtract(resultQuantity, result);

		lsresult = lsresult && lsresult + ''
		return lsresult || '';
	}

	_addFn = () => {
		let { planData } = this.state;
		let lsStr = this._more();
		//数字
		if (MethodUtil.countObj.isNumber(lsStr)) {
			if (lsStr < 0) {
				ToastUtil.showWarning('商品数量未添加或者数量已经超量,不能在添加', 1500)
				return
			}
		} else {
			if (!lsStr) {
				ToastUtil.showWarning('商品数量未添加或者数量已经超量,不能在添加', 1500)
				return
			}
		}

		this.itemData.quantity = lsStr || '';
		let newItemData = Object.assign({}, this.itemData);
		planData.push(newItemData);
		this.setState({ planData });
	}
	_removeFn = (index) => {
		let { planData } = this.state;
		if (planData.length == 1) {
			ToastUtil.showWarning('至少要一个交货计划')
		} else {
			planData.splice(index, 1);
			this.setState({ planData }, () => { this._itemCallback() });
		}
	}

	_itemCallback = (editData, indexpren, type) => {
		let { planData } = this.state;
		let { planDataCall } = this.props.data || {};
		let lsPlanData = [];

		planData && planData.forEach((item, index) => {
			if (indexpren == index) {
				lsPlanData[index] = JSON.parse(JSON.stringify(editData));
			} else {
				lsPlanData.push(item)
			}
		})
		planDataCall && planDataCall(lsPlanData, type)
		this.setState({
			planData: lsPlanData
		})
	}


	render () {
		let { planData } = this.state;
		let { isEdit,isLimit } = this.props.data || {};
		return (
			<View style={orderBaseStyle.box}>
				<TitleCpt
					data={{
						title: '交货计划',
						moreText: isEdit ? '新增计划' : null,
						_moreFn: this._addFn,
					}} />
				{planData.map((item, index) => {
					return <DeliveryCpt isLimit={isLimit} key={index} data={{ index, editData: item, removeFn: this._removeFn, itemCallback: this._itemCallback }} />
				})}
			</View>
		)
	}
}

